package com.zk;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.ArrayList;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;

public class ConsumerDemo {

    //将给定的一批用户里面的名称为"lisi"且年龄大于22岁的用户都给打包起来
    public static void main(String[] args) {

        ArrayList<Person> arrayList = new ArrayList<>();
        Consumer<Person> consumer = x -> {
            if(x.getName().equals("lisi")) {
                arrayList.add(x);
            }
        };


        consumer = consumer.andThen(item -> {
            arrayList.removeIf(x -> x.getAge() < 23);
        });

        Stream.of(
                new Person(21,"zhangsan"),
                new Person(22,"lisi"),
                new Person(23,"wangwu"),
                new Person(24,"wangwu"),
                new Person(23,"lisi"),
                new Person(26,"lisi"),
                new Person(26,"zhangsan")
        ).forEach(consumer);

//        arrayList.removeIf((Predicate<Person>) person -> {
//            return person.getAge() < 23;//过滤23以下的人)
//        });

        System.out.println(JSON.toJSONString(arrayList));  //[{"age":23,"name":"lisi"},{"age":26,"name":"lisi"}]
    }

}

@Data
@Accessors(chain = true)
@AllArgsConstructor
class Person {
    private Integer age;
    private String name;
}